(function($) {
			$.fn.zzsel = function(options) {//options 经常用这个表示有许多个参数。 
			    //定义zzs的构造函数
			    var zzs = function(ele, opt) {
			        this.$element = ele,
			        this.defaults = {
			            'size': 3,
			            'data':{},
			            'html':{},
			            'target':"",
			            'inputName':"",
			            'style':"",
			            'zindex':99999
			        },
			        this.options = $.extend({}, this.defaults, opt)
			    }
				 
			    //加载资质数据
			    var zzsdata = function (o){
			    	var result = null;
			    	$.ajax({
					    type: "GET",
					    url: "/js/qualify/zzcategory.json",
					    async:false,
					    dataType: "json",
					    success: function(data){
					    	 result = data;
					    	 o.options.data=result; 
					    	 o.showcontent()
					    } 
					});
			    
			    }
			    
			    
			  //加载选中数据
			    var seldata= function (o){
			    	var html = "";
			    	var html1 = "";
			    	var id =  $("#"+o.options.inputName).val(); 
			    	var thisobj = '.'+o.options.inputName+'Pop';
			    	var names ="";
			    	var showname="";
			    	//3级分类
			    	if(id.length>4){
			    		var parentId=id.substring(0,2),parentIdTwo=id.substring(0,4);
			    		$(thisobj+' .zz-fl').html(alldata(o,1,0,parentId));
			    		$(thisobj+' .zz-zy').html(alldata(o,2,parentId,parentIdTwo));
		                $(thisobj+' .zz-lb').html(alldata(o,3,parentIdTwo,id)); 
		                $(thisobj+' .zizhi-item span:eq(2)').addClass('active').siblings().removeClass('active');
						$(thisobj+' .zizhi-item span:eq(2)').parent().parent().find('.zizhi-lists div').eq(2).addClass('active').siblings().removeClass('active');
			    	}else if(id.length>2){ //2级分类
			    		var parentId=id.substring(0,2); 
			    		$(thisobj+' .zz-fl').html(alldata(o,1,0,parentId));
			    		$(thisobj+' .zz-zy').html(alldata(o,2,parentId,id));
		                $(thisobj+' .zz-lb').html(""); 
		                $(thisobj+' .zizhi-item span:eq(1)').addClass('active').siblings().removeClass('active');
						$(thisobj+' .zizhi-item span:eq(1)').parent().parent().find('.zizhi-lists div').eq(1).addClass('active').siblings().removeClass('active');
			    	}else{//1级分类
			    		$(thisobj+' .zz-fl').html(alldata(o,1,0,id));
			    		$(thisobj+' .zz-zy').html("");
		                $(thisobj+' .zz-lb').html(""); 
			    	}  
			      return  {html:html,html1:html1};
			    }
 
			    /**
			     * type 1 一级分类  2 2及分类  3 3级分类
			     */
			    var alldata = function (o,type,pid,selid){
			     
			    	var html = "";
			    	if(type==1){
			    		 var category = o.options.data; 
			    		 for(var i =0 ;i<category.length;i++){
			    			 var subdatatwo = category[i].sublist,substate=0;
     						 if(subdatatwo.length>0){
     							substate=1;
     						 } 
     						 var selClass="";
     						 if(selid==category[i].value){
     							selClass="active";
     							 o.$element.val(category[i].name); 
     						 }
	    					 html +='<span sub="'+substate+'"  class="'+selClass+'" data-code="'+category[i].value+'" data-id="'+category[i].value+'" title="'+category[i].name+'"     data-pid="0" >'+category[i].name+'</span>' 
			    		 } 
			    	}else if(type==2){
			    		var data = o.options.data;   
			    		 for(var j = 0 ;j<data.length;j++){
			    			   var parent = data[j].value;
		    				   if(parent== pid){
				   				   var sublist = data[j].sublist;   
					   				for(var i =0 ;i<sublist.length;i++){
					   					var name = sublist[i].name;
	        							if(name.length>15){
	     				   					name = name.substring(0,15)+"...";
	     				   				}
	        							var subdatatwo = sublist[i].sublist,substate=0;
		        						if(subdatatwo.length>0){
		        							substate=1;
		        						} 
		        						 var selClass="";
		         						 if(selid==sublist[i].value){
		         							selClass="active";
		         							 o.$element.val(sublist[i].name); 
		         						 } 
	     				   				html +=' <span class="'+selClass+'" data-code="'+sublist[i].value+'" sub="'+substate+'" data-id="'+sublist[i].value+'"   data-pid="'+sublist[i].value.substring(0,2)+'"  title="'+sublist[i].name+'"    alt="'+sublist[i].name+'"   >'+name+'</span> ';	
					   				} 
				   			   } 
			    		 }
			    	}else{
			    		var data = o.options.data;   
			    		 for(var j = 0 ;j<data.length;j++){
		   				   var sublist = data[j].sublist;   
			   				for(var i =0 ;i<sublist.length;i++){
			   					var name = sublist[i].name;
			   					if(sublist[i].value== pid){  
			   						var subdatatwo = sublist[i].sublist;
	        						if(subdatatwo.length>0){
	    							   for(var l=0;l<subdatatwo.length;l++){
	    								   name = subdatatwo[l].name;
	    								   if(name.length>15){
	    					   					name = name.substring(0,15)+"...";
	    					   			   }
	    								   var selClass="";
			         						 if(selid==subdatatwo[l].value){
			         							selClass="active";
			         						    o.$element.val(subdatatwo[l].name); 
			         						 }
	    					   			   html +=' <span class="'+selClass+'" data-code="'+subdatatwo[l].value+'" data-id="'+subdatatwo[l].value+'"   data-pid="'+subdatatwo[l].value.substring(0,4)+'"  title="'+subdatatwo[l].name+'"    alt="'+subdatatwo[l].name+'"   >'+name+'</span> ';	
	    							   }
	        						}  
			   					} 
			   				} 
				   			 
			    		 }
			    	} 
			    	return html;
			    }
			    
			    
			  //加载页面显示
			    var zzshtml = function(o){
			    	var html =new Array();
			    	   
			    	   if(o.options.style.length>0){
			    		   html +='       <div class="zizhi-wrap '+o.options.inputName+'Pop" style="'+o.options.style+'">'
			    	   }else{
			    		   html +='       <div class="zizhi-wrap '+o.options.inputName+'Pop" style="margin:38px 0 0 0;text-align:left;">'
			    	   }
			    	   html +='         		<h5 class="zizhi-item clefix">'
			    	   html +='         			<span class="active">分类</span><span>专业</span><span>类别</span>'
			    	   html +='         			<i class="'+o.options.inputName+'Close" style="margin-right: 10px;padding-right: 1px;">×</i>'
			    	   html +='    </h5>'
			    	   html +='    <div class="zizhi-lists">'
			    	   html +='    <div class="zz-fl active">'
			    	   html +=alldata(o,1)	    
			    	   html +='    </div>'
			    	   html +='    <div class="zz-zy">'
			    	   html +='    </div>'
			    	   html +='    <div class="zz-lb">'
			           html +='    </div>'
			           html +='    </div>'
			           html +='    </div>'	   
                       o.options.html =html;
			    }
			     
			  //定义zzs的方法
			    zzs.prototype = {
			    		reloadvalue:function(){
				        	var o = this;
				        	seldata(o); 
				        	
				    },
			        init: function() {
			               zzsdata(this); 
			        },showcontent:function(){
			        	 zzshtml(this); 
			               var o = this;
			               var popObj =null;
			               var jobHtmlDt = this.options.html;
			               var thisobj = '.'+this.options.inputName+'Pop';
			               var thisclose =  '.'+this.options.inputName+'Close';
			               $("#"+this.options.inputName).after(jobHtmlDt);
			               seldata(o);//加载选中
			               o.$element.on("click",function(){ 
			            		$(thisobj).toggle();
								$('.ZZmask').toggle(); 
			               });
			               //关闭
			               $(document).on('click',thisclose,function(){
			            		$(thisobj).toggle();
								$('.ZZmask').toggle();
			               }); 
			               //遮罩
			               $('.ZZmask').on('click',function(){
			            	    $(thisobj).hide();
								$(this).hide();
							});
			               /**
			                * 一级分类点击
			                */
			               $(document).on('click',thisobj+' .zz-fl span',function(){
		                       provinVal = $(this).text(),
		                       id  = $(this).attr("data-id"),substate = $(this).attr("sub"),listHtml =  alldata(o,2,id,0);
			            	   $(this).addClass('active').siblings().removeClass('active');
			            	   $(thisobj+' .zz-zy').html("");
			            	   $(thisobj+' .zz-lb').html("");//清空三级分类
			            	   if(substate==1){
				            	   $(thisobj+' .zz-zy').html(listHtml);
				            	   //标签页选中
								   $(thisobj+' .zizhi-item span:eq(1)').addClass('active').siblings().removeClass('active');
								   $(thisobj+' .zizhi-item span:eq(1)').parent().parent().find('.zizhi-lists div').eq(1).addClass('active').siblings().removeClass('active');
			            	   }else{
			            		   var val = $(this).attr("title");
				              	   var code = $(this).attr("data-code");
				              	   o.$element.val(val); 
					           	   $("#"+o.options.inputName).val(code); 
					               $(thisobj).toggle();
								   $('.ZZmask').toggle();
			            	   } 
			               })
			               //二级分类
			                $(document).on('click',thisobj+' .zz-zy span',function(){
								 $(this).addClass('active').siblings().removeClass('active');  
								id  = $(this).attr("data-id"),substate = $(this).attr("sub");
								//有子类时 显示子类 否则插入值
								if(substate==1){
									$(thisobj+' .zizhi-item span:eq(2)').addClass('active').siblings().removeClass('active');
									$(thisobj+' .zizhi-item span:eq(2)').parent().parent().find('.zizhi-lists div').eq(2).addClass('active').siblings().removeClass('active');
									listHtml =  alldata(o,3,id,0);
									$(thisobj+' .zz-lb').html(listHtml);
								}else{
									   $(thisobj+' .zz-lb').html("");//清空三级分类
				            		   var val = $(this).attr("title");
					              	   var code = $(this).attr("data-code");
					              	   o.$element.val(val); 
						           	   $("#"+o.options.inputName).val(code); 
						               $(thisobj).toggle();
									   $('.ZZmask').toggle();
				            		   
				            	   }
			                })
			                //3级分类
			                $(document).on('click',thisobj+' .zz-lb span',function(){
								$(this).addClass('active').siblings().removeClass('active');  
		            		    var val = $(this).attr("title");
			              	    var code = $(this).attr("data-code");
			              	    o.$element.val(val); 
				           	    $("#"+o.options.inputName).val(code); 
				                $(thisobj).toggle();
							    $('.ZZmask').toggle(); 
			                }) 
			               //标签页
			               $(thisobj).on('click','.zizhi-item span',function(){
								var index = $(this).index();
								$(this).addClass('active').siblings().removeClass('active');
								$(this).parent().parent().find('.zizhi-lists div').eq(index).addClass('active').siblings().removeClass('active');
							}); 
			        }
			    }
			    var zzobj = new zzs(this, options);
			    zzobj.init();
				return  zzobj;
			}
		 
		})(jQuery); 

 
 
 